var MemberWl = (function($) {

	var betlistLink = function(cellvalue, options, rowObject) {
		return '<a class="bet-list-link" href="javascript:;" data-user-id="'
				+ rowObject.user_id + '" data-username="' + rowObject.username
				+ '">' + cellvalue + '</a>';
	};

	var userIdModel = {
		name : 'user_id',
		index : 'user_id',
		width : 60,
		sorttype : "int",
		hidden : true

	};
	var userNameModel = {
		name : 'username',
		index : 'username',
		width : 60,
		sorttype : "string",
		formatter : betlistLink
	};
	var memberWlModel = {
		name : 'member_win_lose',
		index : 'member_win_lose',
		width : 80,
		align : "right",
		sorttype : "float",
		formatter : 'currency'
	};
	var agentWlModel = {
		name : 'agent_win_lose',
		index : 'agent_win_lose',
		width : 80,
		align : "right",
		sorttype : "float",
		formatter : 'currency'
	};
	var affiliateWlModel = {
		name : 'affiliate_win_lose',
		index : 'affiliate_win_lose',
		width : 80,
		align : "right",
		sorttype : "float",
		formatter : 'currency'
	};
	var hostWlModel = {
		name : 'host_win_lose',
		index : 'host_win_lose',
		width : 80,
		align : "right",
		sorttype : "float",
		formatter : 'currency'
	};
	return {
		initMemberWlEvent : function() {
			$("#submit").click(function() {
				MemberWl.bindMemberWl();
				return false;
			});

			/* open betlist */
			$("body").on(
					"click",
					".bet-list-link",
					function() {
						location.href = "index.php/admin/report/betlist"
								+ "?user_id=" + $(this).attr("data-user-id")
								+ "&username=" + $(this).attr("data-username")
								+ "&fromdate=" + $("#fromdate").val()
								+ "&todate=" + $("#todate").val();
					});
		},
		initMemberWl : function() {
			if ($("#show_filter").val() == "1") {
				Report.initDateRangePicker();
			}
			MemberWl.initMemberWlEvent();
			MemberWl.bindMemberWl();
		},
		bindMemberWl : function() {

			var gridOption = $.extend(Report.gridOption, {
				height : 500,
				colNames : [ 'User Id', 'User', 'MemberWL', 'AgentWL',
						'AffiliateWl', 'HostWl' ],
				colModel : [ userIdModel, userNameModel, memberWlModel,
						agentWlModel, affiliateWlModel, hostWlModel ]
			});

			$.blockUI();
			$.ajax( {
				type : "POST",
				url : "index.php/admin/report/memberwlData",
				dataType : "json",
				data : {
					fromdate : $("#fromdate").val(),
					todate : $("#todate").val(),
					agent_id : $("#agent_id").val(),
					affiliate_id : $("#affiliate_id").val()
				},
				success : function(response) {
					// Show the grid
					gridOption.data = response.Main;
					gridOption.userData = response.Total;

					$("#jqgrid-table").jqGrid('GridUnload');
					$("#jqgrid-table").jqGrid(gridOption);
					Report.initColumnChooser();
				},
				complete : function() {
					$.unblockUI()
				}
			});
		}

	};
})(jQuery);
